﻿<?php 
if(isset($_REQUEST['hdn_page'])){
	$v_page=intval($_REQUEST['hdn_page']);
}else{
	$v_page=1;
}
$v_qa_content = _replace_bad_char($_REQUEST['txt_qa_content']);
$v_answer_qa_content = _replace_bad_char($_REQUEST['txt_answer_content']);
if($v_qa_content == ""){?>
	<script>
		alert("Phải xác định nội dung câu hỏi");
		window.history.back();
	</script><?php
	exit();	
}
if($v_answer_qa_content ==""){?>
	<script>
		alert("Phải xác định nội dung câu hỏi");
		window.history.back();
	</script><?php
	exit();	
}
if (!_check_permission_on_function($_SESSION['staff_id'], $_ISA_APP_CODE,_CONST_ONEGATE_QA_UPDATE)){?>
	<script>
		alert("<? echo _CONST_FUNCTION_PERMISSION_ERROR; ?>");
		window.history.back();	
	</script><?
	exit;
}
$v_url = "index.php?modal_dialog_mode=1" . "&hdn_item_id=" . $v_qa_id . "&fuseaction=DISPLAY_SINGLE_QA_QUESTION" ;
$v_fuseaction_back="DISPLAY_ALL_QA_QUESTION";

$v_agency_id = 0;
if(isset($_REQUEST['hdn_agency_id'])){
	$v_agency_id = intval($_REQUEST['hdn_agency_id']);
}
$v_agency = "";
if(isset($_REQUEST['hdn_agency'])){
	$v_agency = $_REQUEST['hdn_agency'];
}
$v_filter = "";
if(isset($_REQUEST['hdn_filter'])){
	$v_filter = _replace_bad_char($_REQUEST['hdn_filter']);
}
//Lay cac thong tin ve trang thai cua trang
$v_exception_qa_id  = 0;
if(isset($_REQUEST['hdn_exception_qa_id'])){
	$v_exception_qa_id = intval($_REQUEST['hdn_exception_qa_id']);
}
$v_direction  = 'TOP';
if(isset($_REQUEST['hdn_direction'])){
	$v_direction = $_REQUEST['hdn_direction'];
}
$v_top_id  =   0;
if(isset($_REQUEST['hdn_top_id'])){
	$v_top_id = intval($_REQUEST['hdn_top_id']);
} 
$v_bottom_id = 0;
if(isset($_REQUEST['hdn_bottom_id'])){
	$v_bottom_id = intval($_REQUEST['hdn_bottom_id']);
}
$v_qa_id = 0;
if(isset($_REQUEST['hdn_qa_id'])){
	$v_qa_id = intval($_REQUEST['hdn_qa_id']);
}
//Luu ten cua linh vuc 
$v_cate = "";
if(isset($_REQUEST['hdn_cate'])){
	$v_cate = _replace_bad_char($_REQUEST['hdn_cate']);
} 
$v_cate_id = 0;
if(isset($_REQUEST['hdn_cate_id'])){
	$v_cate_id = intval($_REQUEST['hdn_cate_id']);
}

//Luu trang thai hien thoi
$v_status = -1;
if(isset($_REQUEST['hdn_status'])){	
	$v_status = $_REQUEST['hdn_status'];
}
$v_qa_cate_id = 0;
if(isset($_REQUEST['hdn_qa_cate_id'])){
	$v_qa_cate_id = intval($_REQUEST['hdn_qa_cate_id']);
}
$v_qa_agency_id = 0;
if(isset($_REQUEST['hdn_qa_agency_id'])){
	$v_qa_agency_id = intval($_REQUEST['hdn_qa_agency_id']);
}
$v_qa_sender_name = _replace_bad_char($_REQUEST['txt_qa_sender_name']);
$v_qa_send_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['txt_qa_send_date']);
//$v_qa_answerer_id = intval($_REQUEST['hdn_qa_answerer_id']);
$v_qa_answerer_id =  _get_item_attr_by_id($_SESSION['arr_all_staff'], $_SESSION['staff_id'], 'id');
$v_qa_answerer = _replace_bad_char($_REQUEST['txt_qa_answer_name']);
$v_qa_answered_date = _ddmmyyyy_to_yyyymmdd(($_REQUEST['txt_qa_answered_date']));
$v_qa_status = 0;
if(isset($_REQUEST['hdn_qa_status'])){
	$v_qa_status = intval($_REQUEST['hdn_qa_status']);
}

$v_qa_title = trim(_replace_bad_char($_REQUEST['txt_qa_question_title']));
// Noi dung cau tra loi
$v_qa_answer_content = _replace_bad_char(trim($_REQUEST['txt_answer_content']));
$v_qa_approver_id = $_REQUEST['hdn_approver_id'];
$v_deleted_exist_file_id_list=$_REQUEST['hdn_deleted_exist_file_id_list'];
$v_deleted_new_file_id_list=$_REQUEST['hdn_deleted_new_file_id_list'];
$v_save_and_add_new = 0;
$v_answerer_name = "";
if(isset($_REQUEST['txt_answerer'])){
	$v_answerer_name = $_REQUEST['txt_answerer'];
}

// Xy ly file dinh kem
if(_is_sqlserver()){
	//Du lieu MSSQL
	for($i=0; $i<_CONST_MAX_NUMBER_OF_ATTACH_FILE; $i++){
		$v_form_field = 'file_attach'.$i;
		if (isset($_FILES[$v_form_field]['tmp_name'])){
			if ($v_deleted_new_file_id_list=="" Or _list_have_element($v_deleted_new_file_id_list, $i, ",")==false){
				$v_filename = _replace_bad_char(trim($_FILES[$v_form_field]['name']));
				$v_tmp_filename    = $_FILES[$v_form_field]['tmp_name'];
				$v_file_id = 0;
				if ($v_tmp_filename!="" and !is_null($v_tmp_filename)){
					_copy_file($v_tmp_filename, _CONST_LEVEL1_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					if ($v_new_file_id_list ==""){
						$v_new_file_id_list = _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL1_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					}else{			
						$v_new_file_id_list = $v_new_file_id_list . ",". _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL1_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					}	
				}	
			}	
		}	
	}
	$v_question_text_id = _save_text_to_database('T_TEMP_FILE', 'C_TEXT', $v_qa_content);
	$v_answer_text_id = _save_text_to_database('T_TEMP_FILE', 'C_TEXT', $v_answer_qa_content);	

	//Cap nhat noi dung cau hoi
	$sql = "Onegate_QAQuestionUpdate " . $v_qa_id . "," ;
	$sql = $sql . $v_qa_agency_id . "," ;
	$sql = $sql . $v_qa_cate_id . ",'" ;
	$sql = $sql . $v_qa_answerer_id . "','" ;
	$sql = $sql . $v_qa_sender_name . "','" ;
	$sql = $sql . $v_qa_answered_date . "','";
	$sql = $sql . $v_qa_title . "'," ;
	$sql = $sql . $v_question_text_id. "," ;
	$sql = $sql . $v_answer_text_id. "," ;
	$sql = $sql . $v_qa_status.",'" ;
	$sql = $sql . $v_deleted_exist_file_id_list. "','" ;
	$sql = $sql . $v_new_file_id_list . "'";
	$sql = $sql . ",'".$v_answerer_name . "'";	
	//echo $sql;exit;
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$rs = $ado_conn->GetRow($sql); 
	$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	//Kiem tra xem co check vao phe chuan xuat ban	
	if ($v_qa_status == 2) {  
		$v_qa_approver_id =  $_SESSION['staff_id'];
		$sql = "Onegate_QAQuestionApprove " . $v_qa_id . "," . $v_qa_approver_id . "," . $v_qa_status;
		//echo $sql; exit;
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
		$rs = $ado_conn->GetRow($sql); 
		$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	}  //End if
		if (!is_null($v_error) and $v_error<>""){?>
			<script>
				alert("<? echo $v_error; ?>");
				if (_MODAL_DIALOG_MODE==1){
					window.location = "<? echo $v_url; ?>";
				}else{
					window.history.back();
				}	
			</script><?
			exit;
		}
}
//End is_sqlserver
?>
<form action="index.php" method="post" name="f_back">
	<input type="hidden" name="fuseaction" value="DISPLAY_ALL_QA_QUESTION">
	<input type="hidden" name="hdn_qa_status" value="<?php echo $v_qa_status; ?>">
	<input type="hidden" name="hdn_status" value="<?php echo $v_status; ?>">
	<input type="hidden" name="hdn_filter" value="<?php echo $v_filter; ?>">
	<input type="hidden" name="hdn_cate" value="<?php echo $v_cate;?>">
	<input type="hidden" name="hdn_cate_id" value="<?php echo $v_cate_id;?>">
	<input type="hidden" name="hdn_agency" value="<?php echo $v_agency;?>">
	<input type="hidden" name="hdn_agency_id" value="<?php echo $v_agency_id;?>">
	<input type="hidden" name="hdn_qa_question_cate_id" value="<?php echo $v_qa_question_cate_id;?>">
	<input type="hidden" name="hdn_top_id" value="<?php echo $v_top_id; ?>">
	<input type="hidden" name="hdn_bottom_id" value="<?php echo $v_bottom_id ?>">
	<input type="hidden" name="hdn_direction" value="<?php echo $v_direction; ?>">
	<input type="hidden" name="hdn_exception_qa_id" value="<?php echo $v_exception_qa_id; ?>">
	<input type="hidden" name="hdn_current_item_id" value="<?php echo $v_qa_id;?>">
	<input type="hidden" name="hdn_answer_qa_content" value="<?php echo $v_answer_qa_content; ?>">
	 <input type="hidden" name="hdn_qa_content" value="<?php echo $v_qa_content; ?>">
 	<input type="hidden" name="hdn_deleted_exist_file_id_list" value="<? v_deleted_exist_file_id_list ?>">
	<input type="hidden" name="hdn_deleted_new_file_id_list" value="<? v_deleted_new_file_id_list ?>">	
	<input type="hidden" name="hdn_page" value="<?php echo $v_page;?>">
</form> 
<Script language="javascript">	
	document.forms(0).submit();
</Script>
